<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the Closure to execute when that URI is requested.
|
*/

Route::get('/', function()
{
	return View::make('hello');
});

/**
 * 用来请求创建branch_story的页面
 */
Route::get('/create',function()
{
	$branch_story_no=Input::get('branch_story_no');

	$branch_story=DB::table("branch_story")
								->where('branch_story_no',$branch_story_no)
								->first();

	$games_name = DB::table('games')
						->where('games_no',$branch_story->games_no)
						->pluck('games_name');

	//branch_story_choice存在,返回该branch_story的展示页面
	if($branch_story->branch_story_choice)
	{
		$story_content = DB::table('story')->where('branch_story_no',$branch_story_no)
									->pluck('story_content');

		$branch_story->branch_story_choice = explode('&*-*&', $branch_story->branch_story_choice);

		foreach ($branch_story->branch_story_choice as $key => $value) {
			$branch_story->branch_story_choice[$key] = explode('&*_*&', $branch_story->branch_story_choice[$key]);
		}

		return View::make('show')->with(array(
										'branch_story_choice' => $branch_story->branch_story_choice,
										'story_content' => $story_content,
										'games_name' => $games_name
									));
	}
	//branch_story_choice不存在,返回该branch_story的创建页面
	else
	{
		return View::make('create')->with(array(
										'branch_story_no'=>$branch_story_no,
										'games_name' => $games_name
										));
	}
});
/**
 * 用来进行创建branch_story的数据库操作
 */
Route::post('/create',function()
{
	$branch_story_no=Input::get('branch_story_no');

	$games_no = DB::table('branch_story')
						->where('branch_story_no',$branch_story_no)
						->pluck('games_no');

	$story_content=Input::get('story_content');   //story表的插入
	DB::table('story')->insert(
			array(
					'story_content'=>$story_content,
					'branch_story_no'=>$branch_story_no
					));	

	$input=Input::except('story_content','branch_story_no');  //choice的处理
	$branch_story_choice_arr=array();
	foreach($input as $key=>$in)
	{
		if($in)
		{
			$no=DB::table('branch_story')->insertGetId(	
								array(
									'games_no' => $games_no
									));
			$branch_story_choice_arr[$key]=$no.'&*_*&'.$in;
		}
	}
	$branch_story_choice = implode('&*-*&', $branch_story_choice_arr);
	DB::update('update branch_story set branch_story_choice=? where branch_story_no=?',array($branch_story_choice,$branch_story_no));

	return Redirect::to("create?branch_story_no=$branch_story_no");	
});

Route::get('/play',function()
	{
		if(Input::has('games_no'))
		{	
			$games_no=Input::get('games_no');
			$branch_story_start_no=DB::table('games')->where('games_no',$games_no)->pluck('branch_story_start_no');
			$branch_story_no=$branch_story_start_no;
		}
		else
		{
			$branch_story_no=Input::get('branch_story_no');
			$games_no=DB::table('branch_story')->where('branch_story_no',$branch_story_no)->pluck('games_no');
		}
		$games_name=DB::table('games')->where('games_no',$games_no)->pluck('games_name');
		
		$branch_story_choice=DB::table('branch_story')
							->where('branch_story_no',$branch_story_no)
							->pluck('branch_story_choice');
		if($branch_story_choice)
		{
		$branch_story_choice = explode('&*-*&', $branch_story_choice);

		foreach ($branch_story_choice as $key => $value) {
			$branch_story_choice[$key] = explode('&*_*&', $branch_story_choice[$key]);
		}

		$story_content=DB::table('story')->where('branch_story_no',$branch_story_no)->lists('story_content');
		
		return View::make('story')
					->with(array('story_content'=>$story_content,
								'branch_story_choice'=>$branch_story_choice,
								'games_name'=>$games_name
								));
		
		}
		else
			echo "未完待续";
	});
route::get('/cc',function()
{

	$math=Input::get('math');
	echo $math;
	echo "hello";

	
});


route::get("/dd",function()
{
	echo "hello";
});